package gem.model.course;

import gem.database.Common;
import gem.database.ReaderWriter;

import java.util.Vector;

public class Department
{
	private String m_DepartmentTitle;
	private int m_DepartmentID;
	private boolean m_IsModified;
	
	// In department we will track association table
	// ( depId, sdId, cId )
	private java.util.Set< Association > m_Associations;
	
	private java.util.Vector< ScienceDomain > m_ScienceDomains;
	//====================================================================================//
	public java.util.Set<Association> getAssociations() { return m_Associations; }
	//====================================================================================//
	public Department( String depTitle, int depId )
	{
		m_DepartmentID = depId;
		m_DepartmentTitle = depTitle;
				
		m_ScienceDomains = new Vector<ScienceDomain>();
		
		setModified( false );
	}
	//====================================================================================//
	// used for NEW Department
	public Department( String title )
	{
		this.m_DepartmentTitle = title;
		this.m_DepartmentID = Common.DB_NEW_ENTRY; // Not currently set to DB record, indicates new Department
		
		m_ScienceDomains = new Vector<ScienceDomain>();
		
		setModified( false );
	}
	//====================================================================================//
	public void addScienceDomain( ScienceDomain s )
	{
		if( m_ScienceDomains.contains( s ) )
		{
			return;
		}
		
		Department prevDep = s.getDepartment();
		// first, we have to fetch previous parent, and
		// 1. to remove science domain from it
		// 2. to update the associations
		// if prevDep == null, nothing of 1. and 2. has to be done
		// because it is indicating that science domain is new.
		if( prevDep != null ) // first
		{
			prevDep.deleteScienceDomain( s , false );
			
			Association.departmentChanged( s, prevDep, this );
		}
		m_ScienceDomains.add( s );		
		
		s.setDepartment( this );
		
		setModified( true );
	}
	//====================================================================================//
	//====================================================================================//
	// Deletes ScienceDomain from DB
	public void deleteScienceDomain( ScienceDomain s, boolean deleteFromDB )
	{
		m_ScienceDomains.remove( s );		
	}
	//====================================================================================//
	//====================================================================================//
	public ScienceDomain getScienceDomain( String name )
	{
		for( ScienceDomain sd : m_ScienceDomains )
		{
			if( sd.getTitle().equals(name))
			{
				return sd;
			}
		}
		return null;
	}
	//====================================================================================//
	public ScienceDomain getScienceDomain( int id )
	{
		for( ScienceDomain sd : m_ScienceDomains )
		{
			if( sd.getId() == id )
			{
				return sd;
			}
		}
		return null;
	}
	//====================================================//
	public ScienceDomain getScienceDomain( ScienceDomain sd )
	{
		for( ScienceDomain s : m_ScienceDomains )
		{
			if( s.equals(sd))
			{
				return s;
			}
		}
		return null;
	}
	//====================================================//
	// Ovo je inicijalna funkcija. Njom se vezuje vektor naucnih oblasti
	// za katedru
	//====================================================//
	public void setScienceDomains( java.util.Vector<ScienceDomain> sdomains)
	{
		m_ScienceDomains = sdomains;		
	}
	//====================================================//
	//====================================================//
	public java.util.Vector<ScienceDomain> getScienceDomains()
	{
		return m_ScienceDomains;
	}
	//====================================================//
	public String getTitle() { return m_DepartmentTitle; }
	public int getId() { return m_DepartmentID; }
	public void setId( int id )
	{
		this.m_DepartmentID = id;
	}
	//====================================================//
	public void setTitle( String title )
	{
		m_DepartmentTitle = title;
	}
	//====================================================//
	public boolean getIsModified() { return m_IsModified; }
	public void setModified( boolean modified ) { m_IsModified = modified; }
	//====================================================//
	public void save()
	{
		// Stores permanently whole Department tree
		ReaderWriter.save( this, true );
	}
	//====================================================//
	@Override
	public String toString()
	{
		return this.m_DepartmentTitle;
	}
	//====================================================//
	@Override
	public boolean equals(Object obj)
	{
		if( obj instanceof Department == false)
		{
			return false;
		}
		if( this == obj )
		{
			return true;
		}
		Department temp = (Department)obj;
		return ((temp.getId() == this.getId()) && (temp.getTitle().equals(this.getTitle())));
	}
	//====================================================//
	@Override
	public int hashCode()
	{
		return this.getTitle().hashCode();
	}
	
}
